﻿using System;
using System.Collections.Generic;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	
	class FormFormatEmptyElementWorker
	{
		
		public static void  formatEmptyElement(Form form)
		{
			XmlDocument doc = form.HtmlDoc;
			XmlElement root = doc.DocumentElement;
            //CID 0827 //2011.03.07 //Begin
            List<XmlElement> elementList = new List<XmlElement>();
            collectElements(root, elementList);
            //CID 0827 //2011.03.07 //End
			XmlElement formElement = null;
			foreach (XmlElement element in elementList)
			{
				if (!element.HasChildNodes)
				{
					//CID 0279 //2009.04.29 //Begin
					XmlElement parentElement = element.ParentNode as XmlElement;
					if( formElement != null )
					{
						if( parentElement.Equals( formElement ) )
						{
							continue;
						}
					}
					else
					{
						if( parentElement.GetAttribute( "id" ).Equals( JetConstant.FormId ) && element.Name.Equals( JetConstant.HTML_input ) )
						{
							formElement = parentElement;
							continue;
						}
					}
					//CID 0279 //2009.04.29 //End
					//CID 0372 //2009.08.03 //Begin
					if( element.Name.ToLower().Equals( "br" ) )
					{
						continue;
					}
					//CID 0372 //2009.08.03 //End
					element.InnerText = "";
				}
			}
		}

        //CID 0827 //2011.03.07 //Begin
        private static void collectElements(XmlElement element, List<XmlElement> elementList)
        {
            elementList.Add(element);
            foreach (XmlNode node in element.ChildNodes)
            {
                if (node is XmlElement)
                {
                    XmlElement childElement = (XmlElement)node;
                    collectElements(childElement, elementList);
                }
            }
        }
        //CID 0827 //2011.03.07 //End
	}
}